// order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {   
    var that = this;
    that.setData({
      options: options,
    })
  },
  onShow: function () {
    var app = getApp()
    var that = this;
    var uid = wx.getStorageSync('uid');
    // console.log(that.data.options.book_time);
    // console.log(that.data.options.book_date);
    // 内容
    wx.request({
      url: app.globalData.host + '&g=App&m=Order&a=orderShow',
      data: {
        id: that.data.options.id,
        uid: uid    
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {        
        console.log(res.data)
        that.setData({
          oid: res.data.oid,
          ad: res.data.ad,
          cp: res.data.cp,
        })
      }
    })
    that.setData({
      isHide: 1,
    })
  },
  // 判断switch是否选中
  bindchecked:function(e){
    console.log('switch1 发生 change 事件，携带值为', e.detail.value)
    console.log(e)
    var that = this;
    var k = e.currentTarget.dataset.k;
    var cp = that.data.cp;  
    cp[k].chose = e.detail.value   
    console.log(cp)
    that.setData({     
      cp: cp,
    })
    
    // var sbtn = e.datail.value
    // console.log(e.datail.value)
    // wx.request({
    //   url: app.globalData.host + '&g=App&m=Order&a=saveOrder',
    //   data: {
    //     id: that.data.options.id,
    //   },
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   success: function (res) {
    //     var ad = res.data.ad;
    //     console.log(res.data)
    //     that.setData({
        
    //     })
    //   }
    // })
  },
  // 支付
  neworder: function () {
    const that = this;
    var app = getApp()
    var uid = wx.getStorageSync('uid');
    var cp = that.data.cp;  
    var j = 0;   
    var coupon = '';
    for (let i = 0; i < cp.length; i++) {
      if (cp[i].chose) {
        coupon += (j?',':'')+cp[i].id
        j++;
      }
    }
    
    console.log(coupon)
    // return false
    wx.request({
      url: app.globalData.host + '&g=App&m=Order&a=saveOrder',
      data: {   
        uid: uid,     
        id: that.data.options.id, 
        coupon: coupon
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
       // return false
        if (res.data.status == 1) {
          wx.request({
            url: app.globalData.host + '&g=App&m=Order&a=wx_p1',
            data: { oid: that.data.options.id },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              if (res.data.status == 1) {
                wx.showToast({
                  title: '支付成功',
                  icon: 'loading',
                  duration: 2000,
                  complete: function (res1) {
                    //page/mall/comfirm_course
                    wx.navigateTo({
                      url: '/page/member/record',
                      success: function (resun) {
                        that.onUnload()
                      }
                    })
                  }
                })
                return false;
              }
              //console.log(res)
              wx.requestPayment({
                'appId': res.data.appId,
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.package,
                'signType': 'MD5',
                'paySign': res.data.paySign,
                'success': function (res) {
                  console.log('success');
                  wx.showToast({
                    title: '支付成功',
                    icon: 'loading',
                    duration: 2000,
                    complete: function (res1) {
                      //page/mall/comfirm_course
                      
                      wx.navigateTo({
                        url: '/page/member/record',
                        success: function (resun) {
                          that.onUnload()
                        }
                      })
                    }
                  })
                  // wx.navigateTo({
                  //   url: ''
                  // })
                },
                'fail': function (res) {
                  console.log(res);
                  console.log('fail');
                },
                'complete': function (res) {
                  console.log(res);
                  console.log('complete');
                }
              })
            }
          })
        } else {
          wx.showToast({
            title: res.data.info,
            icon: 'cancle',
            duration: 2000
          })
          return false
        }
      }
    });
  },

})